//the par object
function par(audiosrc, clipbegin, clipend, textsrc, ids)
{
	this.audioSrc=audiosrc;	
	this.clipBegin = clipbegin;
	this.clipEnd = clipend;
	this.textSrc = textsrc;
	this.idValues = ids;
}

//the smil parser and player
var smil = 
{
	playbackIndex: 0,
	smilData: null,
	smilFilePath: null,

	//localFile is an nsILocalFile type
	parseSmil: function(localFile)
	{
		var isPar = false;
		//the file as a stream
		var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].
					            createInstance(Components.interfaces.nsIFileInputStream);
		var xmlReader = Components.classes["@mozilla.org/saxparser/xmlreader;1"]
		                          .createInstance(Components.interfaces.nsISAXXMLReader);
	
		fstream.init(localFile, -1, 0, 0);
		var count = 0;
		this.smilData = new Array();
	  	var parid, textsrc, textid, audiosrc, clipbegin, clipend, audioid, ids, pardata;
	
		xmlReader.contentHandler = 
		{
			startElement: function(uri, localName, qName, /*nsISAXAttributes*/ attributes) 
			{
				if (qName == "par")
				{
					isPar = true;
					parid = attributes.getValueFromQName("id");
				}
				if (isPar == true)
				{
					if (qName == "audio")
					{
						audiosrc = attributes.getValueFromQName("src");
						clipbegin = attributes.getValueFromQName("clip-begin"); 
						clipend = attributes.getValueFromQName("clip-end");
						audioid = attributes.getValueFromQName("id");
					}
					else if (qName == "text")
					{
						textsrc = attributes.getValueFromQName("src");
						textid = attributes.getValueFromQName("id");
					}
				}
			},

			endElement: function(uri, localName, qName) 
			{
		    	if (qName == "par")
				{
					isPar = false;
					ids = new Array(parid, textid, audioid);
					pardata = new par(audiosrc, clipbegin, clipend, textsrc, ids);
					this.smilData[count] = pardata;
					count++;
				}
			},
		
			startDocument: function() {
			
			},
			endDocument: function() {			
			},
			characters: function(value) {
				
			},
			processingInstruction: function(target, data) {
				
			},
			ignorableWhitespace: function(whitespace) {
				
			},
			startPrefixMapping: function(prefix, uri) {
				
			},
			endPrefixMapping: function(prefix) {
				
			},

			// nsISupports
			QueryInterface: function(iid) 
			{
			    if(!iid.equals(Components.interfaces.nsISupports) &&
			       !iid.equals(Components.interfaces.nsISAXContentHandler))
			      throw Components.results.NS_ERROR_NO_INTERFACE;
			    return this;
			}
		};
		xmlReader.parseFromStream(fstream,"", "text/xml");
	},

	//should check if the file is already open
	open: function(filepath)
	{	
		this.smilFilePath = filepath;
		this.parseSmil(filepath, "");
	},
	
	playAtId: function(id)
	{
		//find the ID
		if (id == "") return;
	
		var found = false;
		for (var i = 0; i<this.smilData.length; i++)
		{
			//each chunk of SMIL data has several IDs associated with it, so look at all of them
			for (var j = 0; j<this.smilData[i].idValues.length; j++)
			{
				if (this.smilData[i].idValues[j] == id)
				{
					found = true;
					this.playbackIndex = i;
					break;
				}
			}
			if (found) break;
		}		
		if (found) this.playFromCurrentIndex();
	},

	playFromCurrentIndex: function()
	{
		this.play(this.smilData[this.playbackIndex]);
	},

	//start playing from the beginning of the currently loaded smil file
	playFirstPhrase: function()
	{
		this.playbackIndex = 0;
	  	this.playFromCurrentIndex();
	},

	playLastPhrase: function()
	{
		this.playbackIndex = this.smilData.length - 1;
		this.playFromCurrentIndex();
	},
	
	playNextPhrase: function()
	{
		this.playbackIndex++;
		if (this.playbackIndex >= this.smilData.length)
		{
			book.loadNextInSpine();
		}
		else
		{
			this.playFromCurrentIndex();
		}
	},

	playPrevPhrase: function()
	{
		this.playbackIndex--;
		if (this.playbackIndex < 0)
		{
			book.loadPrevInSpine();
		}
		else
		{
			this.playFromCurrentIndex();
		}
	},

	//this function sends requests for data to be rendered
	//the textfile/audiofile paths are sent as absoute paths
	play: function(parData)
	{
		alert("smil file: " + this.smilFilePath);
		
		//show text
		var textfile = Components.classes["@mozilla.org/file/local;1"].
		            createInstance(Components.interfaces.nsILocalFile);
		textfile.initWithFile(this.smilFilePath.parent);
		textfile.appendRelativePath(parData.textSrc);
		alert("text will be: " + textfile.path);
		
		//play audio
		var audiofile = Components.classes["@mozilla.org/file/local;1"].
					createInstance(Components.interfaces.nsILocalFile);
		audiofile.initWithFile(this.smilFilePath.parent);
		audiofile.appendRelativePath(parData.audioSrc);			
		alert("audio will be: " + audiofile.path);
		
		
	  	showTextThen(textfile, function() {
	    	playAudio(audiofile, parData.clipBegin, parData.clipEnd, this.playNextPhrase);
	  	});
	}
}